package bookStore.dao.privilege;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import bookStore.dao.DaoTemplate;
import bookStore.po.User;

public class UserDaoImpl extends DaoTemplate implements UserDao{

	public UserDaoImpl(Connection connection) {
		super(connection);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<User> queryAll(int offset, int pageSize) {
		this.sql="select * from users limit ?,?";
		try {
			return this.queryRunner.query(connection, sql, new BeanListHandler<User>(User.class), offset,pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void save(User t) {
		this.sql="insert into users values(null,?,?,?)";
		try {
			this.queryRunner.update(connection,sql,t.getUsername(),t.getPassword(),t.getPicture());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public User get(int id) {
		
		try {
			return this.queryRunner.query(connection, "select * from users where id = ?", new BeanHandler<User>(User.class), id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void update(User t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User login(String username, String password) {
		this.sql="select * from users where username=? and password=?";
		try {
			return this.queryRunner.query(connection, sql, new BeanHandler<User>(User.class), username,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void register(User t){
		this.sql="insert into users values(null,?,?,?)";
		try {
			this.queryRunner.update(connection,sql,t.getUsername(),t.getPassword(),t.getPicture());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public User getUserByName(String username) {
		this.sql="select * from users where username=?";
		try {
			return this.queryRunner.query(connection, sql, new BeanHandler<User>(User.class), username);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getInsertedId() {
		// TODO Auto-generated method stub
		this.sql="select last_insert_id()";		
		try {
			return this.queryRunner.query(connection, sql, new ScalarHandler<Long>(1));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return -1;
	}

}
